<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>计数器</title>
    <script src="../vue.js"></script>
    <script src="../vuex.js"></script>
</head>
<body>
    <div id="app">
        <div>
            <button @click="calc">count</button>
            <span>{{this.$store.state.count}}</span>
        </div>
    </div> 

    <script>

        const store=new Vuex.Store({
            state:{
                count:0
            },
            mutations:{
                increment(state){
                    state.count++
                },

                init_count(state,count){
                    state.count=count
                }

            },
            actions:{
                add({commit}){
                    commit('increment')
                },

                first_count(context){
                    context.commit('init_count',5)
                }
            }
        })

        var vm=new Vue({
            el:"#app",
            store,
            beforeMount(){
                this.$store.dispatch('first_count')
            },

            methods:{
                calc(){
                    this.$store.dispatch('add')
                }
            }

        })

    </script>
</body>
</html>